package com.next.servlet;

import com.bean.Book;
import com.bean.History;
import com.bean.HistoryReturn;
import com.bean.User;
import com.dao.BookDao;
import com.dao.HistoryDao;
import com.dao.NextHistoryDao;
import com.utils.PageUtil;
import com.utils.ResponseUtils;
import com.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * @ClassName HistoryServlet
 * @Desc 后台历史数据控制层
 * @Author xiaoding
 * @Date 2021/1/16 21:31
 * @Version 1.0
 */
@WebServlet(name = "NextHistoryServlet",value = "/NextHistoryServlet")
public class HistoryServlet extends HttpServlet {
   private NextHistoryDao historyDao = new NextHistoryDao();
    private BookDao bookDao = new BookDao();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取参数，判断功能属性
        String str = request.getParameter("flag");

        //选择功能属性
        if (str.equals("selectBorrow")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //定义状态
            String flag = "已借阅";

            //查询所有借阅图书的数量
            int count = historyDao.historyCount(flag);
            //定义访问的Servlet
            String targetUrl = "NextHistoryServlet";
            //定义参数
            String param = "flag=selectBorrow";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySelect(new HistoryReturn(flag ,page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/bookLending.jsp").forward(request, response);
        }else if (str.equals("selectReturn")) {
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }
            //定义状态
            String flag = "已归还";

            //查询所有借阅图书的数量
            int count = historyDao.historyCount(flag);
            //定义访问的Servlet
            String targetUrl = "NextHistoryServlet";
            //定义参数
            String param = "flag=selectReturn";
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySelect(new HistoryReturn(flag ,page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/bookReturn.jsp").forward(request, response);
        }else if (str.equals("update")) {
            //获取借阅记录的Id号
            int hId = Integer.parseInt(request.getParameter("hId"));
            //设置状态
            String flag = "已归还";
            //设置状态、路径
            String message = null;
            String url = null;
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (StringUtils.isNotNull(request.getParameter("page"))) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //调用归还书籍的方法
            int a = historyDao.historyReturn(new HistoryReturn(flag,hId));

            //判断是否归还成功
            if (a > 0) {
                message = "归还成功";
            }else {
                message = "归还失败";
            }

            //设置搜索名称
            String bName = null;
            //判断是否是默认查询或根据名称查询
            if (StringUtils.isNotNull(request.getParameter("bName"))) {
                bName = request.getParameter("bName");
                url = "NextHistoryServlet?flag=searchBorrow&page="+page+"&bName="+bName;
            }else {
                url = "NextHistoryServlet?flag=selectBorrow&page="+page;
            }
            //跳转页面
            ResponseUtils.writeMessage(response,message,url);
        }else if (str.equals("searchBorrow")) {
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置状态
            String flag = "已借阅";

            //查询所有借阅图书的数量
            int count = historyDao.historySearchCount(new HistoryReturn(flag,"%" + bName + "%"));
            //定义访问的Servlet
            String targetUrl = "NextHistoryServlet";
            //定义参数
            String param = "flag=searchBorrow&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySearch(new HistoryReturn(flag,"%" + bName + "%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/bookLending.jsp").forward(request, response);
        }else if (str.equals("searchReturn")) {
            //获取图书名称
            String bName = request.getParameter("bName");
            //设置当前页
            int page = 1;
            //判断是否是第一次查询
            if (request.getParameter("page") != null) {
                page = Integer.parseInt(request.getParameter("page"));
            }

            //设置状态
            String flag = "已归还";

            //查询所有借阅图书的数量
            int count = historyDao.historySearchCount(new HistoryReturn(flag, "%" + bName + "%"));
            //定义访问的Servlet
            String targetUrl = "NextHistoryServlet";
            //定义参数
            String param = "flag=searchReturn&bName="+bName;
            //调用获取分页的方法
            String pageUtil = PageUtil.genPagination(targetUrl, count, page, PageUtil.pageSize, param);
            //调用查询数据的方法
            List<History> history = historyDao.historySearch(new HistoryReturn(flag,"%" + bName + "%",page,PageUtil.pageSize));

            //设置会话
            request.setAttribute("history",history);
            request.setAttribute("pageUtil",pageUtil);

            //跳转页面
            request.getRequestDispatcher("next/bookReturn.jsp").forward(request, response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
